package com.store.service.impl;

import com.store.entity.Cart;
import com.store.entity.Product;
import com.store.mapper.CartMapper;
import com.store.mapper.ProductMapper;
import com.store.service.CartService;
import com.store.service.ex.*;
import com.store.vo.CartVO;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 购物车业务层
 */
@DubboService(interfaceClass = CartService.class,version = "1.0.0",timeout = 5000)
public class CartServiceImpl implements CartService {

    @Resource
    private CartMapper cartMapper;

    @Resource
    private ProductMapper productMapper;

    /**
     * 查询用户
     * @param uid 用户id
     * @return 购物车集合
     */
    @Override
    public List<CartVO> getCartByUid(Integer uid) {
        List<CartVO> exists = cartMapper.selectCartByUid(uid);
        if(exists == null){
            throw new CartNotFoundException("购物车数据不存在");
        }
        return  exists;
    }

    /**
     * 加入购物车
     * @param uid 用户id
     * @param username 执行者
     * @param id 商品id
     */
    @Override
    public void addCart(Integer uid, String username, Integer id) {
        Product exists = productMapper.selectProduct(id);
        if(exists == null){
            throw new ProductNotFoundException("目标商品不存在异常");
        }else {
            //封装数据
            Cart cart = new Cart();
            cart.setUid(uid);
            cart.setPid(id);
            cart.setPrice(exists.getPrice());
            cart.setNum(1);
            cart.setCreatedUser(username);
            cart.setCreatedTime(new Date());
            cart.setModifiedUser(username);
            cart.setModifiedTime(new Date());
            int rows = cartMapper.addCart(cart);
            if(rows != 1){
                throw new InsertException("添加购物车数据时产生未知的异常");
            }
        }
    }

    /**
     * 更新购物车数量
     * @param cid 购物车id
     * @param username 执行者
     * @param num 新的数量
     */
    @Override
    public void UpdateNumByCid(Integer cid, String username, Integer num) {
        Cart exists = cartMapper.selectCartByCid(cid);
        if(exists == null){
            throw new CartNotFoundException("购物车数据找不到异常");
        }else {
            //封装数据
            Cart cart = new Cart();
            cart.setCid(cid);
            cart.setNum(num);
            cart.setModifiedUser(username);
            cart.setModifiedTime(new Date());
            int rows = cartMapper.updateNumByCid(cart);
            if(rows != 1){
                throw new UpdateException("更新购物车数据时产生未知的异常");
            }
        }
    }

    /**
     * 删除购物车数据
     * @param cid 购物车id
     */
    @Override
    public void deleteCartByCid(Integer cid) {
        Cart exists = cartMapper.selectCartByCid(cid);
        if(exists == null){
            throw new CartNotFoundException("购物车数据不存在!");
        }
        int rows = cartMapper.deleteCartByCid(cid);
        if(rows != 1){
            throw new DeleteException("删除购物车数据出现未知的异常");
        }
    }

}
